﻿using System;
using System.Collections.Generic;
using FileSecurityManager.BizEntities;
using FileSecurityManager.DataAccess.Interfaces;

namespace FileSecurityManager.DataAccess.Repositories
{
    using System.Linq;

    using FileSecurityManager.DataAccess.Exceptions;

    public class RoleLinq2SqlRepository : Linq2SqlRepository, IRoleRepository
    {
        #region IRepository implementation

        /// <summary>
        /// Creates the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>Item model</returns>
        public RoleModel Create(RoleModel model)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the model by id.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>Item model</returns>
        public RoleModel GetById(int id)
        {
            try
            {
                var role =
                    (from dbRole in FSMDatabase.Roles where dbRole.Id == id select dbRole).FirstOrDefault().ToModel(FSMDatabase);
                return role;
            }
            catch (Exception ex)
            {
                var errorMsg = string.Format("Error retrieving user by id: {0}. Error: {1}", id, ex.Message);
                Log.Error(errorMsg, ex);
                throw new DataAccessException(errorMsg, ex);
            }
        }

        /// <summary>
        /// Gets model by name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>The model</returns>
        public RoleModel GetByName(string name)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Updates the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        public void Update(RoleModel model)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the specified id.
        /// </summary>
        /// <param name="id">The identifier.</param>
        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets all models.
        /// </summary>
        /// <returns>All models list</returns>
        public List<RoleModel> GetAll()
        {
            try
            {
                var users =
                    (from dbRole in this.FSMDatabase.Roles select dbRole.ToModel(this.FSMDatabase)).ToList();

                return users;
            }
            catch (Exception ex)
            {
                var errorMsg = string.Format("Error retrieving roles: {0}", ex.Message);
                Log.Error(errorMsg, ex);
                throw new DataAccessException(errorMsg, ex);
            }
        }
        #endregion
    }
}
